<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Admin extends Controller
{
    public function index()
    {
        // 查询所有数据
        $list = Db::name('admin')->field(['id', 'name','power'])->order('id', 'desx')->select();
        // dump($list);

        // // 赋值并加载模版
        // $this->assign('title', '用户列表');
        // $this->assign('list', $list);
        // $this->fetch('user/index');

        // 助手函数
        return view('admin/index', [
            'list' => $list,
        ]);

    }
    public function login()
    {
        $p = Request::instance()->post();
//        dump($p);exit;
        $name = $p['name'];

        //$list = query("select password from admin where name = ".$p['name']);
        $list = Db::table('admin')->where('name',"$name")->value('password');
        //var_dump($list);exit;
        if (md5($p['password']) == $list) {
            $_SESSION['admin']['name'] = $name;
//            var_dump($_SESSION);exit;
            return view('index/index');
        } else {
            return $this->success('密码错误',url('admin/index/login'));
        }


//        if (!md5($p['password'] == $list)) {
//
//        }
    }
    public function create()
    {
//        $data = ['name'=>'safe','password'=>'123456'];
//        Db::table('admin')->insert($data);
//        Db::table('admin')->insertGetId($data);
        $p = Request::instance()->post();
        $data = [
            'name'=>$p['name'],
            'password'=>md5($p['password'])
//            'repass'=>md5($p['repass'])
        ];
//        var_dump($data);
//        if (!$data['password'] == $data['repass']) {
//               return '两次密码不一致';
//        }
        $result = Db::table('admin')->insert($data);
        if ($result > 0) {
            return $this->success('注册成功',url('admin/index/login'));
        } else {
            return $this->success('注册失败',url('admin/index/login'));
        }
    }

    public function add()
    {

    }

}
